package com.cynteka.cyfoman.core;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * User: Sergey
 * Date: 02.08.11
 * Time: 15:35
 */
public class FilePropertyResolver implements IPropertyResolver {

    Logger logger = Logger.getLogger(FilePropertyResolver.class.getName());

    public static final String NAME_PN = "name";
    public static final String BASENAME_PN = "baseName";
    public static final String ORIGINALNAME_PN = "originalName";
    public static final String FULLPATH_PN = "fullPath";
    public static final String SIZE_PN = "size"; // only for files
    public static final String FULL_SIZE_PN = "fullSize"; // Also for directories
    public static final String EXT_PN = "extension";
    public static final String PATH_PN = "path";
    public static final String ISHIDDEN_PN = "isSystem";
    public static final String ISFILE_PN = "isFile";
    public static final String ISDIRECTORY_PN = "isDirectory";
    public static final String LASTMODIFIED_PN = "lastModified";
    public static final String CONTENT_PN = "content";

    @Override
    public Object getProperty(Object o, String propertyName) {
        File file = null;
        if (o instanceof File)
            file = (File) o;
        if (file == null)
            return null;
        if (NAME_PN.equals(propertyName)) {
            return FilenameUtils.getName(file.getName());
        } else if (BASENAME_PN.equals(propertyName)) {
            return FilenameUtils.getBaseName(file.getName());
        } else if (ORIGINALNAME_PN.equals(propertyName)) {
            return file.getName();
        } else if (FULLPATH_PN.equals(propertyName)) {
            return file.getAbsolutePath();
        } else if (SIZE_PN.equals(propertyName)) {
            if (file.isFile())
                return file.length();
            return null;
        } else if (FULL_SIZE_PN.equals(propertyName)) {
            return FileUtils.sizeOf(file);
        } else if (EXT_PN.equals(propertyName)) {
            return FilenameUtils.getExtension(file.getName());
        } else if (PATH_PN.equals(propertyName)) {
            return FilenameUtils.getPath(file.getName());
        } else if (ISHIDDEN_PN.equals(propertyName)) {
            return file.isHidden();
        } else if (ISFILE_PN.equals(propertyName)) {
            return file.isFile();
        } else if (ISDIRECTORY_PN.equals(propertyName)) {
            return file.isDirectory();
        } else if (LASTMODIFIED_PN.equals(propertyName)) {
            return new Date(file.lastModified());
        } else if (CONTENT_PN.equals(propertyName)) {
            String res = null;
            try {
                res = FileUtils.readFileToString(file);
            } catch (IOException e) {
                // TODO investigate if it's ok
                logger.log(Level.WARNING, "Cannot read content file : ", e);
            }
            return res;
        }
        return null;
    }
}
